import {getRemoteExampleVideo} from '@remotion/example-videos';
import {expect, test} from 'bun:test';
import {getTfraBoxes} from '../containers/iso-base-media/traversal';
import {mediaParserController} from '../controller/media-parser-controller';
import {nodeReader} from '../node';
import {parseMedia} from '../parse-media';
import type {IsoBaseMediaStructure} from '../parse-result';

test(
	'should parse mfra atoms',
	async () => {
		const video = await getRemoteExampleVideo('fragmentedMoofTrickyDuration');

		const controller = mediaParserController();

		const {slowStructure} = await parseMedia({
			src: video,
			reader: nodeReader,
			acknowledgeRemotionLicense: true,
			controller,
			fields: {
				durationInSeconds: true,
				slowStructure: true,
			},
		});

		expect(
			getTfraBoxes((slowStructure as IsoBaseMediaStructure).boxes).length,
		).toBe(2);
		expect(
			getTfraBoxes((slowStructure as IsoBaseMediaStructure).boxes)[0],
		).toEqual({
			offset: 8731723,
			boxSize: 1240,
			type: 'tfra-box',
			entries: [
				{
					time: 1024,
					moofOffset: 1272,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 16384,
					moofOffset: 12482,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 20992,
					moofOffset: 25532,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 36352,
					moofOffset: 94095,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 51712,
					moofOffset: 241735,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 67072,
					moofOffset: 355739,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 82432,
					moofOffset: 490611,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 97792,
					moofOffset: 586370,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 113152,
					moofOffset: 674532,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 128512,
					moofOffset: 713330,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 143872,
					moofOffset: 771926,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 150016,
					moofOffset: 802392,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 165376,
					moofOffset: 832655,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 180736,
					moofOffset: 880227,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 196096,
					moofOffset: 914385,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 209920,
					moofOffset: 943825,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 225280,
					moofOffset: 974849,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 240640,
					moofOffset: 1020488,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 256000,
					moofOffset: 1064325,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 263680,
					moofOffset: 1094851,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 279040,
					moofOffset: 1133409,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 284672,
					moofOffset: 1162569,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 300032,
					moofOffset: 1214780,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 315392,
					moofOffset: 1262371,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 330752,
					moofOffset: 1328542,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 346112,
					moofOffset: 1379735,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 361472,
					moofOffset: 1524456,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 376832,
					moofOffset: 1723072,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 392192,
					moofOffset: 1929933,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 407552,
					moofOffset: 2193774,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 422912,
					moofOffset: 2497779,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 425984,
					moofOffset: 2609134,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 441344,
					moofOffset: 2828118,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 456704,
					moofOffset: 3093746,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 472064,
					moofOffset: 3290635,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 487424,
					moofOffset: 3466821,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 502784,
					moofOffset: 3664479,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 518144,
					moofOffset: 3798153,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 533504,
					moofOffset: 3929096,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 548864,
					moofOffset: 4120540,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 564224,
					moofOffset: 4205900,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 579072,
					moofOffset: 4309443,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 594432,
					moofOffset: 4350518,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 597504,
					moofOffset: 4395081,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 608256,
					moofOffset: 4584251,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 620544,
					moofOffset: 4736589,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 635904,
					moofOffset: 4964132,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 651264,
					moofOffset: 5211262,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 666624,
					moofOffset: 5400365,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 681984,
					moofOffset: 5606942,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 697344,
					moofOffset: 5729930,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 712704,
					moofOffset: 5807730,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 718336,
					moofOffset: 5904177,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 733696,
					moofOffset: 6188245,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 749056,
					moofOffset: 6594949,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 760320,
					moofOffset: 6932347,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 775680,
					moofOffset: 7217468,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 788480,
					moofOffset: 7335712,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 803840,
					moofOffset: 7672243,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 819200,
					moofOffset: 7797447,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 834560,
					moofOffset: 8041037,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 849920,
					moofOffset: 8290472,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 854528,
					moofOffset: 8521975,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
				{
					time: 869888,
					moofOffset: 8652448,
					trafNumber: 1,
					trunNumber: 1,
					sampleNumber: 1,
				},
			],
			trackId: 1,
		});
	},
	{
		timeout: 1000000,
	},
);
